package pers.xiaoliang.concurrency.example.concurrent;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.DefaultManagedAwareThreadFactory;

import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @description: 线程池的使用
* @author: xiaoliang.liu
* @date: 2018/7/29 13:44
* @version v1.0
*/
@Slf4j
public class ThreadPoolExample1 extends HashMap {


    public static void main(String[] args) throws Exception {

        // 创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                21,
                42,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100),
                new DefaultManagedAwareThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        for (int i = 0; i < 10; i++) {
            int finalI = i;
            threadPoolExecutor.execute(() -> log.info("thread running {}", finalI));
        }
        threadPoolExecutor.shutdown();

        // 定时执行
//        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
//        scheduledExecutorService.scheduleAtFixedRate(() -> {
//            log.info("scheduledThread running {}");
//        }, 1, 3,  TimeUnit.SECONDS);

    }


}
